package com.kun.comic;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.Dialog;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.text.InputType;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;

import com.kun.comic.extra.Ultils;
import com.kun.comic.extra.ZoomImageView;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.assist.FailReason;
import com.nostra13.universalimageloader.core.listener.ImageLoadingListener;

public class Reader extends Activity {

	private List<String> linkImage = new ArrayList<String>();
	private ViewPager viewPager;
	private TextView txt;
	private ImageButton btnPre, btnNext;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_reader);
		getActionBar().setDisplayHomeAsUpEnabled(true);
		getActionBar().setIcon(android.R.color.transparent);
		Intent intent = getIntent();
		final String link = intent.getStringExtra("link");
		final String name = intent.getStringExtra("name");
		setTitle(name);
		txt = (TextView) findViewById(R.id.txt);
		txt.setText(name);
		viewPager = (ViewPager) findViewById(R.id.viewpager);
		btnPre = (ImageButton) findViewById(R.id.prev);
		btnNext = (ImageButton) findViewById(R.id.next);

		btnPre.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
			}
		});

		btnNext.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {

			}
		});
		new asynInit(name, link).execute();
	}

	class asynInit extends AsyncTask<Void, Void, String> {

		private String name, link;

		public asynInit(String name, String link) {
			this.name = name;
			this.link = link;

		}

		@Override
		protected void onPreExecute() {
			Ultils.CreateDialog(Reader.this);
		};

		@Override
		protected String doInBackground(Void... params) {
			init(link, name);
			return null;
		}

		@Override
		protected void onPostExecute(String result) {
			Ultils.DimissDialog();
			viewPager.setAdapter(new ViewAdapter(linkImage.size()));
		};
	}

	@SuppressLint("DefaultLocale")
	private void init(String link, String name) {
		try {
			Document doc = Jsoup.connect(link).get();
			Elements img = doc.select("img[src]");
			linkImage.clear();
			for (Element element : img) {
				String res = element.attr("src");
				if (res.toLowerCase().contains(".png")
						|| res.toLowerCase().contains(".jpg")) {
					linkImage.add(res);
				}
			}
		} catch (Exception ex) {
			System.out.println(ex.getMessage());

		}
	}

	private class ViewAdapter extends PagerAdapter {
		@SuppressWarnings("unused")
		int size;

		public ViewAdapter(int size) {
			this.size = size;
		}

		@Override
		public int getCount() {
			return linkImage.size();
		}

		@Override
		public boolean isViewFromObject(View arg0, Object arg1) {
			return (arg0 == arg1);
		}

		@Override
		public Object instantiateItem(ViewGroup container, int position) {
			final ZoomImageView img = new ZoomImageView(Reader.this, null);
			ImageLoader.getInstance().displayImage(linkImage.get(position),
					img, new ImageLoadingListener() {

						@Override
						public void onLoadingStarted(String arg0, View arg1) {
							img.setBackgroundResource(R.drawable.ic_load);
						}

						@Override
						public void onLoadingFailed(String arg0, View arg1,
								FailReason arg2) {
							img.setBackgroundResource(R.drawable.ic_noimage);
						}

						@Override
						public void onLoadingComplete(String arg0, View arg1,
								Bitmap arg2) {
							img.setBackgroundResource(android.R.color.transparent);
						}

						@Override
						public void onLoadingCancelled(String arg0, View arg1) {

						}

					});
			((ViewPager) container).addView(img, 0);
			return img;

		}

		@Override
		public void destroyItem(ViewGroup container, int position, Object object) {
			((ViewPager) container).removeView((ZoomImageView) object);
		}
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		getMenuInflater().inflate(R.menu.reader, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		int id = item.getItemId();
		if (id == android.R.id.home) {
			finish();
		}
		if (id == R.id.gochap) {
			OpenDialogGoto();
		}
		return super.onOptionsItemSelected(item);
	}

	private void OpenDialogGoto() {
		final Dialog dialog = new Dialog(Reader.this);
		dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);

		LinearLayout lin = new LinearLayout(Reader.this);
		lin.setOrientation(LinearLayout.HORIZONTAL);
		lin.setLayoutParams(new LayoutParams(300, LayoutParams.WRAP_CONTENT));
		dialog.setContentView(lin);
		final EditText ed = new EditText(getApplicationContext());
		ed.setLayoutParams(new LayoutParams(150, LayoutParams.MATCH_PARENT));
		ed.setTextColor(Color.parseColor("#000000"));
		ed.setSingleLine();
		ed.findFocus();
		ed.setInputType(InputType.TYPE_CLASS_NUMBER);
		Button btn = new Button(getApplicationContext());
		btn.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
				LayoutParams.WRAP_CONTENT));
		btn.setBackgroundColor(Color.parseColor("#cd3a3e"));
		btn.setText("Goto");
		lin.addView(ed);
		lin.addView(btn);

		btn.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				if (!ed.getText().equals("")) {

				} else {
					Toast.makeText(getApplicationContext(),
							"Plz enter number!", Toast.LENGTH_SHORT).show();
				}

			}
		});
		dialog.show();
	}
}
